﻿namespace Yom.Common
{
    using System.IO;
    using System.Text;

    /// <summary>
    /// 文件操作类
    /// 
    /// 创建: 邱柯皓 2009/02/04 19:52
    /// </summary>
    public static class Fso
    {
        /// <summary>
        /// 检测文件是否存在
        /// 
        /// 创建: 邱柯皓 2009/02/04 19:58
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        /// <returns>返回检测结果</returns>
        public static bool Exists(string FilePath)
        {
            FileInfo info = new FileInfo(FilePath);

            return info.Exists;
        }

        /// <summary>
        /// 复制文件
        /// 
        /// 创建: 邱柯皓 2009/02/04 20:03
        /// </summary>
        /// <param name="OldPath">原始文件路径</param>
        /// <param name="NewPath">新文件路径</param>
        public static void CopyFile(string OldPath, string NewPath)
        {
            if (Exists(OldPath))
            {
                DeleteFile(NewPath);
                new FileInfo(Factory.MapPath(OldPath)).CopyTo(Factory.MapPath(NewPath), true);
            }
            else
            {
                new FileInfo(Factory.MapPath(OldPath)).CopyTo(Factory.MapPath(NewPath), true);
            }
        }

        /// <summary>
        /// 移动文件
        /// 
        /// 创建: 邱柯皓 2009/02/04 20:05
        /// </summary>
        /// <param name="OldPath">原始文件路径</param>
        /// <param name="NewPath">新文件路径</param>
        public static void MoveFile(string OldPath, string NewPath)
        {
            string path = Path.GetDirectoryName(NewPath);
            if (!Exists(path))
                CreateDirectory(path);
            if (Exists(OldPath))
            {
                DeleteFile(NewPath);
                new FileInfo(Factory.MapPath(OldPath)).MoveTo(Factory.MapPath(NewPath));
            }
            else
            {
                new FileInfo(Factory.MapPath(OldPath)).MoveTo(Factory.MapPath(NewPath));
            }
        }

        /// <summary>
        /// 删除文件
        /// 
        /// 创建: 邱柯皓 2009/02/04 20:06
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        public static void DeleteFile(string FilePath)
        {
            FileInfo info = new FileInfo(Factory.MapPath(FilePath));

            if (info.Exists)
                info.Delete();
        }

        /// <summary>
        /// 创建文件夹
        /// 
        /// 创建: 邱柯皓 2009/02/04 20:08
        /// </summary>
        /// <param name="DirPath">文件夹路径</param>
        public static void CreateDirectory(string DirPath)
        {
            string path = Factory.MapPath(DirPath);
            DirectoryInfo info = new DirectoryInfo(path);

            if (!info.Exists)
                Directory.CreateDirectory(path);
        }

        /// <summary>
        /// 删除文件夹
        /// 
        /// 创建: 邱柯皓 2009/02/04 20:10
        /// </summary>
        /// <param name="DirPath">文件夹路径</param>
        public static void DeleteDirectory(string DirPath)
        {
            DirectoryInfo info = new DirectoryInfo(Factory.MapPath(DirPath));

            if (info.Exists)
                info.Delete(true);
        }

        /// <summary>
        /// 获取文件大小
        /// 
        /// 创建: 邱柯皓 2009/02/04 20:11
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        /// <returns>返回文件大小</returns>
        public static long FileSize(string FilePath)
        {
            FileInfo info = new FileInfo(Factory.MapPath(FilePath));

            if (info.Exists)
                return info.Length;

            return 0;
        }

        /// <summary>
        /// 读取文件内容
        /// 
        /// 创建: 邱柯皓 2009/02/04 20:35
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        /// <returns>返回文件内容</returns>
        public static string ReadFile(string FilePath)
        {
            return ReadFile(FilePath, Encoding.GetEncoding("UTF-8"));
        }

        /// <summary>
        /// 通过指定编码读取文件内容
        /// 
        /// 创建: 邱柯皓 2009/02/04 20:38
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        /// <param name="encode">文件编码</param>
        /// <returns>返回文件内容</returns>
        public static string ReadFile(string FilePath, Encoding Encode)
        {
            if (Exists(FilePath))
            {
                StreamReader st = new StreamReader(FilePath, Encode);
                string strContent = st.ReadToEnd();
                st.Close();
                st.Dispose();

                return strContent;
            }

            return "";
        }

        /// <summary>
        /// 写入文件
        /// 
        /// 创建: 邱柯皓 2009/02/04 20:39
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        /// <param name="Content">文件内容</param>
        public static void WriteFile(string FilePath, string Content)
        {
            WriteFile(FilePath, Content, false, Encoding.GetEncoding("UTF-8"));
        }

        /// <summary>
        /// 写入文件
        /// 
        /// 创建: 邱柯皓 2009/02/04 20:43
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        /// <param name="Content">文件内容</param>
        /// <param name="Append">是否覆盖</param>
        public static void WriteFile(string FilePath, string Content, bool Append)
        {
            WriteFile(FilePath, Content, Append, Encoding.GetEncoding("UTF-8"));
        }

        /// <summary>
        /// 写入文件
        /// 
        /// 创建: 邱柯皓 2009/02/04 20:45
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        /// <param name="Content">文件内容</param>
        /// <param name="Append">是否追加</param>
        /// <param name="Encode">文件编码</param>
        public static void WriteFile(string FilePath, string Content, bool Append, Encoding Encode)
        {
            StreamWriter st = new StreamWriter(FilePath, !Append, Encode);
            st.Write(Content);
            st.Close();
            st.Dispose();
        }
    }
}